* This file creates the data used in the analysis for Figure D1

set more off


* First, create the hourly census of A&E patient numbers
**************************************************


u "$saveddata/data_ae2011.dta", clear
append using "$saveddata/data_ae2012.dta"

rename exitdate exit_date
rename arrivaldate arrival_date

keep exit_date arrival_date site_code arrivalhour exithour

gen month = mofd(arrival_date)
keep site_code arrival* exit* month
qui levelsof site_code, clean
foreach site in `r(levels)' {
	qui levelsof month if site_code=="`site'", clean
	foreach x in `r(levels)' {
		preserve
		
		* hospital-month data only (keep 1 month either side in case of long stays)
		keep if site_code=="`site'" & month>=`x'-1 & month<=`x'

		* create census variables		
		qui levelsof arrival_date  if month==`x', clean
		foreach date in `r(levels)' {
			forv hour = 0/23 {
				gen c_`date'_`hour' = (arrival_date==`date' & exit_date==`date' & arrivalhour<`hour' & exithour>=`hour') | (arrival_date==`date'-1 & exit_date==`date' & exithour>=`hour')
				}
			}
			
		* reshape to panel
		collapse (sum) c_*, by(site_code)
		reshape long c_, i(site_code) j(date_hour) str
		rename c_ census
		
		* save hospital-month panel
		save "$temp_ae/temp_census_panel_`site'_`x'.dta", replace
		
		restore
		}
	}
	
set more off
* append the hospital-month panels to a single panel
local files : dir "$temp_ae" files "temp_census_panel_*.dta"
u "$temp_ae/temp_census_panel_RA201_615.dta", clear
foreach file in `files' {
	append using "$temp_ae/`file'"
	*erase "`temp_ae'/`file'"
	}
duplicates drop

* date and hour variables
gen date = substr(date_hour,1,5)
gen hour = substr(date_hour,7,.)
destring date hour, replace
drop date_hour

* double-check duplicates 
duplicates report site_code date hour
assert r(N)==r(unique_value)

* define blockages
rename census ae_census
foreach x in 99 95 90 85 80 75 50 10{
bys site_code: egen ae_threshold`x' = pctile(ae_census), p(`x')
gen ae_block`x' = ae_census>ae_threshold`x'
}

save "$saveddata/panel_census_ae.dta", replace



* Create a measure of shocks to each ED


use "$saveddata/data_complete_withcosts.dta"

gen arrival = 1
gen hour  = arrivalhour
gen date = arrivaldate

collapse (sum) arrival, by(hour date site_code)

merge 1:1 hour date site_code using "$saveddata/panel_census_ae.dta"
keep if _merge==3

gen week = wofd(date)
gen month = mofd(date)
gen fyear = yofd(dofm(mofd(date)-3))
gen wofy = week(date)
gen mofy = month(date)
gen dofw = dow(date)
gen t = dhms(date, hour, 0, 0)
format %tc t
format %tw week 
format %tm month

* Drop really small departments (make code crash later)
egen total_arrival = sum(arrival), by(site_code)
drop if total_arrival<1000


/*
unique site_code
unique date
*/

drop ae_threshold99 - _merge

* For each hospital create shocks

* calculate shocks (to arrivals)
gen shock_ymdh = .
gen shock_h = .
rename arrival arrivals
gen larrivals = ln(arrivals)
gen shock_lymdh = .
gen shock_lh = .

* Shocks to census
gen cshock_ymdh = .
gen cshock_h = .
gen lcensus = ln(ae_census)
gen cshock_lymdh = .
gen cshock_lh = .

encode site_code, gen(x)


set more off
set matsize 11000
qui levelsof x, clean


forval i=1(1)160{

	preserve
		keep if x==`i'
		
		qui reg arrivals i.mofy#i.dofw#i.hour#i.fyear if x==`i'
	predict temp, res
	replace shock_ymdh = temp if x==`i'
	drop temp
		
	qui reg arrivals i.hour if x==`i'
	predict temp, res
	replace shock_h = temp if x==`i'
	drop temp
	
	qui reg larrivals i.mofy#i.dofw#i.hour#i.fyear if x==`i'
	predict temp, res
	replace shock_lymdh = temp if x==`i'
	drop temp
		
	qui reg larrivals i.hour if x==`i'
	predict temp, res
	replace shock_lh = temp if x==`i'
	drop temp
	
	* census shock
	qui reg ae_census i.mofy#i.dofw#i.hour#i.fyear if x==`i'
	predict temp, res
	replace cshock_ymdh = temp if x==`i'
	drop temp
		
	qui reg ae_census i.hour if x==`i'
	predict temp, res
	replace cshock_h = temp if x==`i'
	drop temp
	
	qui reg ae_census i.mofy#i.dofw#i.hour#i.fyear if x==`i'
	predict temp, res
	replace cshock_lymdh = temp if x==`i'
	drop temp
		
	qui reg ae_census i.hour if x==`i'
	predict temp, res
	replace cshock_lh = temp if x==`i'
	drop temp
		
		
		save "$temp_ae/aeshock`i'.dta", replace
	restore	
}
*/

set more off
use "$temp_ae/aeshock1.dta", clear
forval x=2(1)180{
append using "$temp_ae/aeshock`x'.dta"
}

* save
sort site_code date hour arrivals ae_census
keep site_code date hour *shock* arrivals ae_census lcensus larrivals

save "$saveddata/panel_aeshocks.dta", replace

* Work out the distribution of these shocks
*******************************************

gen pred_census = ae_census - cshock_ymdh

xtile pred_census100 = pred_census, n(100)

* Denote whether in the top or bottom third of predicted business
gen pred_busy33 = 0 if pred_census100<34
replace pred_busy33 = 1 if pred_census100>66

keep date hour site_code pred_busy33 pred_census pred_census100

save "$saveddata/panel_census_predictedvalues.dta", replace

